package com.tiyo.monitor.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.Deque;
import java.util.List;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiyo.common.utils.StringUtils;
import com.tiyo.framework.shiro.constant.ShiroConstants;
import com.tiyo.framework.shiro.session.OnlineSessionDAO;
import com.tiyo.monitor.mapper.SysUserOnlineMapper;
import com.tiyo.monitor.mapper.domain.SysUserOnline;
import com.tiyo.monitor.service.SysUserOnlineService;
@Service
public class SysUserOnlineServiceImpl implements SysUserOnlineService {
	@Autowired
    private EhCacheManager ehCacheManager;
	@Autowired
	SysUserOnlineMapper sysUserOnlineMapper;
	 @Autowired
    OnlineSessionDAO onlineSessionDAO;
	@Override
	public SysUserOnline selectOnlineById(String sessionId) {
		return sysUserOnlineMapper.selectById(sessionId);
	}

	@Override
	public void deleteOnlineById(String sessionId) {
		sysUserOnlineMapper.deleteById(sessionId);

	}

	@Override
	public void batchDeleteOnline(List<String> sessions) {
		sysUserOnlineMapper.deleteBatchIds(sessions);

	}

	@Override
	public void saveOnline(SysUserOnline online) {
		SysUserOnline _online= sysUserOnlineMapper.selectById(online.getSessionId());
		if(StringUtils.isNull(_online)) {
			sysUserOnlineMapper.insert(online);
		} else {
			sysUserOnlineMapper.updateById(online);
		}

	}

	@Override
	public void removeUserCache(String loginName, String sessionId) {
		Cache<String, Deque<Serializable>> cache = ehCacheManager.getCache(ShiroConstants.SYS_USERCACHE);
        Deque<Serializable> deque = cache.get(loginName);
        if (StringUtils.isEmpty(deque) || deque.size() == 0)
        {
            return;
        }
        deque.remove(sessionId);
		
	}

	@Override
	public IPage<SysUserOnline> selectUserOnlineList(SysUserOnline userOnline) {
		Page<SysUserOnline> page = new Page<SysUserOnline>(1, 10);
		QueryWrapper<SysUserOnline> queryWrapper = new QueryWrapper<SysUserOnline>();
		return sysUserOnlineMapper.selectPage(page, queryWrapper);
	}

	@Override
	public void forceLogout(String sessionId) {
		 Session session = onlineSessionDAO.readSession(sessionId);
        if (session == null)
        {
            return;
        }
        session.setTimeout(1000);
        sysUserOnlineMapper.deleteById(sessionId);

	}

	@Override
	public List<SysUserOnline> selectOnlineByExpired(Date expiredDate) {
		QueryWrapper<SysUserOnline> queryWrapper = new QueryWrapper<SysUserOnline>();
		queryWrapper.le("last_access_time", expiredDate);
		return sysUserOnlineMapper.selectList(queryWrapper);
	}

}
